package com.tour.service.impl;

import com.tour.controller.LoginModel;

import com.tour.dao.*;

import com.tour.model.*;

import com.tour.service.*;

import com.tour.util.*;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.ui.ModelMap;

import java.io.IOException;
import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Service
public class GlInfoServiceImpl implements GlInfoService {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");
    @Autowired
    GlInfoMapper glInfoMapper;
    @Autowired
    TypeInfoMapper typeInfoMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    MsgInfoMapper msgInfoMapper;

    /**
      新增
    */
    @Override
    public String add(GlInfo model, LoginModel login) {
        String check = checkData(model, 1); //检查数据是否符合要求

        if (check.equals("") == false) {
            return check;
        }

        model.setUserId(login.getId()); //登录id
        model.setCheckStatus(1); //默认待审核,
        model.setCreateTime(sdf1.format(new Date())); //当前时间格式
        glInfoMapper.insertSelective(model); //插入数据

        return "";
    }

    /**
      修改
    */
    @Override
    public String update(GlInfo model, LoginModel login) {
        GlInfo preModel = glInfoMapper.selectByPrimaryKey(model.getId());
        String check1 = checkData(model, 2); //检查数据是否合法

        if (check1.equals("") == false) {
            return check1;
        }

        preModel.setTitle(model.getTitle());
        preModel.setContent(model.getContent());
        preModel.setGlType(model.getGlType());
        preModel.setGlLevel(model.getGlLevel());
        glInfoMapper.updateByPrimaryKey(preModel); //更新数据

        return "";
    }

    /**
      审核通过
    */
    @Override
    public String tg(GlInfo model, LoginModel login) {
        GlInfo preModel = glInfoMapper.selectByPrimaryKey(model.getId());
        preModel.setCheckStatus(2); //审核状态设置为审核通过
        preModel.setCheckRemark(model.getCheckRemark());
        glInfoMapper.updateByPrimaryKey(preModel); //更新数据
        
        MsgInfo msg = new MsgInfo();
        msg.setContent("您的文章攻略“"+preModel.getTitle()+"”已审核通过，奖励"+10+"积分");
        msg.setCreateTime(sdf1.format(new Date()));
        msg.setIsRead(1);
        msg.setUserId(preModel.getUserId());
        msgInfoMapper.insertSelective(msg);
        UserInfo user = userInfoMapper.selectByPrimaryKey(preModel.getUserId());
        user.setUserScore(user.getUserScore()+10);
        if(user.getUserScore()>100){
        	user.setUserLevel(2);
        }
        userInfoMapper.updateByPrimaryKeySelective(user);
        

        return "";
    }

    /**
      审核不通过
    */
    @Override
    public String btg(GlInfo model, LoginModel login) {
        GlInfo preModel = glInfoMapper.selectByPrimaryKey(model.getId());
        preModel.setCheckStatus(3); //审核状态设置为审核不通过
        preModel.setCheckRemark(model.getCheckRemark());
        glInfoMapper.updateByPrimaryKey(preModel); //更新数据

        return "";
    }

    /**
    *根据参数查询攻略列表数据
    */
    @Override
    public Map<String, Object> getDataList(GlInfo queryModel, Integer page,
        Integer pageSize, LoginModel login) {
        GlInfoExample se = new GlInfoExample();
        GlInfoExample.Criteria sc = se.createCriteria();
        se.setOrderByClause("id desc"); //默认按照id倒序排序

        if (queryModel.getId() != null) {
            sc.andIdEqualTo(queryModel.getId());
        }

        if ((queryModel.getTitle() != null) &&
                (queryModel.getTitle().equals("") == false)) {
            sc.andTitleLike("%" + queryModel.getTitle() + "%"); //模糊查询
        }

        if (queryModel.getGlType() != null) {
            sc.andGlTypeEqualTo(queryModel.getGlType()); //查询所属板块等于指定值
        }

        if (queryModel.getCheckStatus() != null) {
            sc.andCheckStatusEqualTo(queryModel.getCheckStatus()); //查询审核状态等于指定值
        }

        if (queryModel.getUserId() != null) {
            sc.andUserIdEqualTo(queryModel.getUserId());
        }

        int count = (int) glInfoMapper.countByExample(se);
        int totalPage = 0;

        if ((page != null) && (pageSize != null)) { //分页

            if ((count > 0) && ((count % pageSize) == 0)) {
                totalPage = count / pageSize;
            } else {
                totalPage = (count / pageSize) + 1;
            }

            se.setPageRows(pageSize);
            se.setStartRow((page - 1) * pageSize);
        }

        List<GlInfo> list = glInfoMapper.selectByExample(se); //执行查询语句
        Map<String, Object> rs = new HashMap<String, Object>();
        List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();

        for (GlInfo model : list) {
            list2.add(getGlInfoModel(model, login));
        }

        rs.put("list", list2); //数据列表
        rs.put("count", count); //数据总数
        rs.put("totalPage", totalPage); //总页数

        return rs;
    }

    /**
      封装攻略为前台展示的数据形式
    */
    @Override
    public Map<String, Object> getGlInfoModel(GlInfo model, LoginModel login) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("glInfo", model);

        if (model.getGlType() != null) {
            TypeInfo typeInfo = typeInfoMapper.selectByPrimaryKey(model.getGlType()); //所属板块为外接字段,需要关联板块类型来解释该字段

            if (typeInfo != null) {
                map.put("glTypeStr", typeInfo.getTypeName());
            }
        }

        if (model.getUserId() != null) {
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(model.getUserId()); //发布用户为外接字段,需要关联用户来解释该字段

            if (userInfo != null) {
                map.put("userIdStr", userInfo.getName());
            }
        }

        map.put("glLevelStr",
            DataListUtils.getGlLevelNameById(model.getGlLevel() + "")); //解释观看级别字段
        map.put("checkStatusStr",
            DataListUtils.getCheckStatusNameById(model.getCheckStatus() + "")); //解释审核状态字段

        return map;
    }

    /**
    * 删除数据
    */
    @Override
    public void delete(Integer id) {
        glInfoMapper.deleteByPrimaryKey(id);
    }

    public String checkData(GlInfo model, Integer type) { //type=1 表示新增操作,type=2 表示修改操作,type=3 表示审核通过操作,type=4 表示审核不通过操作,

        if ((type == 1) || (type == 2)) {
            if ((model.getTitle() == null) || model.getTitle().equals("")) {
                return "攻略标题为必填属性";
            }
        }

        if ((type == 1) || (type == 2)) {
            if ((model.getContent() == null) || model.getContent().equals("")) {
                return "攻略文章为必填属性";
            }
        }

        if ((type == 1) || (type == 2)) {
            if (model.getGlType() == null) {
                return "所属板块为必填属性";
            }
        }

        if ((type == 1) || (type == 2)) {
            if (model.getGlLevel() == null) {
                return "观看级别为必填属性";
            }
        }

        return "";
    }
}

